/* Copyright Andy Mitchell 2007. No warranties */

function EventObservers(assocObj){
	this.assocObj = assocObj;
	this.eventObservers = [];
}

EventObservers.prototype.observe = function(el, type, func, bubble){
	// Bind the func to the assocObj; and set up event listener
	// Pack all the arguments into the eventObservers array so it can be retrieved later
	
	if( !el ) return;
	
	arguments[arguments.length] = GIPROTOTYPE.bindAsEventListener( this.assocObj, func );	// Bind and make retrievable
	GIPROTOTYPE.observe( el, type, arguments[4], bubble );
	
	this.eventObservers.push(arguments);
};

EventObservers.prototype.stopObserving = function(el, type, func, bubble){
	var args = null;
	var match = false;
	for( var i = this.eventObservers.length - 1; i >= 0; i-- ){
		args = this.eventObservers[i];
		match = true;
		for( j = 0; j < arguments.length; j++ ){
			if( args[j]!=arguments[j] ){
				match = false;
				break;
			}
		}
		if( match ){
			// Stop observing and remove from array
			GIPROTOTYPE.stopObserving( el, type, args[4], bubble );
			this.eventObservers.splice(i, 1);
		}
	}
};

EventObservers.prototype.reset = function(){
	var args = null;
	for( var i = this.eventObservers.length - 1; i >= 0; i-- ){
		args = this.eventObservers[i];
		GIPROTOTYPE.stopObserving( args[0], args[1], args[4], args[3] );
		delete args[2];
		delete args[4];
	}
	this.eventObservers.length = 0;
}
EventObservers.prototype.destroy = function(){
	if( !this.eventObservers ) return;
	this.reset();
	this.eventObservers = null;
}